﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using clEntidad;

namespace clBD
{
    public class BDLlave:BDBase 
    {
        public BDLlave(OleDbConnection cn):base(cn){ }
        public BDLlave(OleDbConnection cn, OleDbTransaction trx) : base(cn, trx) { }
        public OleDbDataReader get(String codigo)
        {
            String query = "select detalle,cant1,cant2,idLocal, estado, ult_Id, cant3,alerta_en " +
                "from mllave where codigo='" + codigo.Trim().ToUpper() + "'";
            return execDr(query);
        }
        public void grabar(Llave llave)
        {
            String query = "select 1 from mllave where codigo='" + 
                llave.codigo   + "'";
            OleDbDataReader dr = execDr(query);
            if (!dr.HasRows)
            {
                query = "insert into mllave(codigo,detalle,cant1,cant2,idlocal,ult_id,cant3,alerta_en, estado) "
                    + " values ('" + llave.codigo  + "','"
                    + llave.detalle.Trim().ToUpper() + "',"
                    + llave.cant1.ToString() + ","
                    + llave.cant2.ToString() + ","
                    + llave.codLocal.ToString() + ","
                    + llave.ultId.ToString() + ","
                    + llave.cant3.ToString() + ","
                    + llave.alertaEn.ToString() + ",'"
                    + llave.estado + "'" +
                    ")";

            }
            else
            {
                query = "update mllave set detalle='" + llave.detalle.Trim().ToUpper()
                    + "',cant1 =" + llave.cant1.ToString()
                    + ",cant2=" + llave.cant2.ToString()
                    + ",ult_id=" + llave.ultId.ToString()
                    + ",estado='" + llave.estado + "'"
                    + ",cant3=" + llave.cant3.ToString()
                    + ",alerta_en=" + llave.alertaEn.ToString()
                    + " where codigo='" + llave.codigo + "'";
            }
            execute(query);

        }
        public void devolucion(string codigo)
        {
            string update = "update mllave set estado='D' where codigo='" + 
                codigo + "'";
            execute(update);
        }

        public OleDbDataReader getPendientes(int idLocal)
        {
            String query = "select l.codigo, l.detalle, r.num_doc, r.ape_pat,r.ape_mat,r.nombres, r.entrega"
                + " from mllave l, reg_llave r "
                + " where l.estado='E' and l.ult_id=r.id and r.id_loc=" 
                + idLocal.ToString() 
                + " order by r.entrega desc";

            return execDr(query);
        }
    }
}
